﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.IO;
using System.Text;

using CE.Image;
using CE.User;
using CE.Mail;
using CE.Global;
using CE.Database;
using CE.CarInsurance;
using CE.Configuration;

public partial class Email_CompletedQuote : MyLanguageBasePage
{
    string _redirectUrl = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session[CE.Global.General.UserId] == null && Session[CE.Global.General.UserTypeId] == null)
            {
                Response.Redirect("Login.aspx");
            }

            if (Session["ViewCIResultId"] != null)      
            {
                Int64 _tempResultId = 0;
                if (Int64.TryParse(Convert.ToString(Session["ViewCIResultId"]), out _tempResultId))      
                {
                    txtEmail.Text = Session[CE.Global.General.UserEmail].ToString();
                }
                else
                {
                    Response.Redirect("ViewResultHistory.aspx");
                }
            }
            else
            {
                Response.Redirect("ViewResultHistory.aspx");
            }

            txtEmail.Attributes.Add("onblur", "RemoveSpace(this);ShowValidationEffectTwo('" + txtEmail.ClientID + "','" + rfvEmail.ClientID + "','" + rfvEmailValid.ClientID + "');DefaultEmail(this, event);");
            txtEmail.Attributes.Add("onfocus", "ClearText(this, event);");
        }
        else
        {
            lblMessage.Text = "";
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {           
            try
            {
                Int64 _tempResultId = 0;
                Int64 _carId = 0;
                if (Int64.TryParse(Convert.ToString(Session["ViewCIResultId"]), out _tempResultId))     
                {
                    _carId = CheckCompleteStatus(_tempResultId);

                    if (_carId > 0)
                    {
                        ShowDetails(_carId);

                        CompletedQuotePDFOutput _completedQuotePdf = CreateCompletedQuotePDFObject();
                        string _fileName = _completedQuotePdf.GeneratePDF(hidGetStep.Value);
                        if (_fileName != string.Empty)
                        {
                            FileInfo file = new System.IO.FileInfo(_fileName);

                            if (file.Exists)
                            {
                                string[] _file = new string[1];
                                _file[0] = _fileName;

                                string _toemail = txtEmail.Text.Trim().ToLower();
                                string _ccemail = string.Empty;

                                string _bccemail = string.Empty;
                                string _customerName = Session[CE.Global.General.UserName].ToString();
                                string _emailTitle = string.Empty;
                                string mailBody = "";

                                if (CE.Global.General.SetCulture() == "th-TH")
                                {
                                    if (VehicleNo.Trim().Length > 2)
                                    {
                                        _emailTitle = "รายละเอียดประกันรถยนต์ ทะเบียน " + VehicleNo + " - GluayGluay.com ";
                                    }
                                    else
                                    {
                                        _emailTitle = "รายละเอียดประกันรถยนต์ เลขที่อ้างอิง " + QRefNo + " - GluayGluay.com ";
                                    }

                                    mailBody = @"เรียน คุณ " + _customerName +
                                    "<br/><br/><a href='http://www.gluaygluay.com'>Gluaygluay.com</a> ขอส่งรายละเอียดประกันรถยนต์ของคุณ โปรดดูที่ไฟล์แนบ <br/> " +
                                    "หากท่านต้องการสอบถามรายละเอียดเพิ่มเติม กรุณาติดต่อ <strong>GluayGluay.com</strong> ได้ที่หมายเลขโทรศัพท์ <strong>02-268-8222</strong> <br/>วันจันทร์ ถึง วันศุกร์ เวลา 9.00 – 18.00 หรือส่งอีเมล์มาหาเราที่  <strong>support@gluaygluay.com.</strong>";

                                    mailBody += "<br/><br/>ขอแสดงความนับถือ<br/>" +
                                    "<br/>ทีมงาน GluayGluay.com<br/><br/>" +
                                        //Company Name changed
                                    "บริษัท คอมแพร์เอ็กซ์เพรส นายหน้าประกันภัย (ประเทศไทย) จำกัด<br/>" +
                                    "เลขทะเบียนนายหน้าประกันวินาศภัยนิติบุคคล เลขที่ ว00016/2553<br/>" +
                                    "2 อาคาร เค.ซี.ซี. ชั้น 2 สีสมซอย 9 ถนนสีลม เขตบางรัก 10500<br/>" +
                                    "เว็บไซต์: <a href='http://www.GluayGluay.com'>www.GluayGluay.com</a> | โทร.: 02-268 8222 | อีเมล์: support@gluaygluay.com<br/>" +
                                    "Follow us: <a href='http://www.facebook.com/GGmotor'>www.facebook.com/GGmotor</a> | Instagram: #GGmotor<br/>" +
                                    "<br/><br/>" +
                                    "<a href='http://www.gluaygluay.com'><img src='http://www.gluaygluay.com/images/public/logo/gluaygluay-plakat_export.jpg' alt='GluayGluay.com'/></a>";
                                }
                                else
                                {
                                    if (VehicleNo.Trim().Length > 2)
                                    {
                                        _emailTitle = "Completed Car Details [" + VehicleNo + "] on GluayGluay.com ";
                                    }
                                    else
                                    {
                                        _emailTitle = "Completed Car Details [" + QRefNo + "] on GluayGluay.com ";
                                    }
                                    mailBody = @"Dear " + _customerName +
                                    "<br/><br/>Please find attached in PDF a copy of your completed car insurance details on <a href='http://www.gluaygluay.com'>Gluaygluay.com.</a> " +
                                    "<br/><br/>We are ready to assist at <strong>02-268-8222</strong> Monday to Fridays from 9am to 6pm. <br/>Alternatively, you can email us at <strong>support@gluaygluay.com</strong>.";

                                    mailBody += "<br/><br/>Best regards,<br/>" +
                                    "<br/>Support GluayGluay.com<br/><br/>" +
                                    "Comparexpress Insurance Broker ( Thailand ) Ltd,<br/>" +
                                    "OIC Licensed Non-Life Insurance Broker: ว00016/2553<br/>" +
                                    "2 K.C.C Building 2nd floor, Silom Soi9<br/>" +
                                    "Silom Road, Bangrak, Bangkok 10500<br/>" +
                                    "W: <a href='http://www.GluayGluay.com'>www.GluayGluay.com</a> | T: 02-268 8222 | E: support@gluaygluay.com<br/>" +
                                    "Follow us: <a href='http://www.facebook.com/GGmotor'>www.facebook.com/GGmotor</a> | Instagram: #GGmotor<br/>" +
                                    "<br/><br/>" +
                                    "<a href='http://www.gluaygluay.com'><img src='http://www.gluaygluay.com/images/public/logo/gluaygluay-plakat_export.jpg' alt='GluayGluay.com'/></a>";
                                }

                                bool _mailSendingStatus = false;
                               
                                _mailSendingStatus = MyMailManager.SendMailWithAttach(_customerName, _toemail, _ccemail, _bccemail, _emailTitle, mailBody, _file);

                                if (_mailSendingStatus)
                                {
                                    _redirectUrl = "Complete-Quote.aspx";
                                    Response.Redirect(_redirectUrl);
                                }
                                else
                                {
                                    lblMessage.Text = Resources.ErrorMessage.EmailFail; //"Email sending failed.";
                                }
                            }
                            else
                            {
                                lblMessage.Text = Resources.ErrorMessage.PDFFail; //"PDF generating failed.";
                            }
                        }
                        else
                        {
                            lblMessage.Text = Resources.ErrorMessage.PDFNoItem; //"PDF generating failed. No item found.";
                        }
                    }
                }
            }
            catch (Exception Ex)
            {

                lblMessage.Text = Ex.Message;
            }
        }
    }

    private CompletedQuotePDFOutput CreateCompletedQuotePDFObject()
    {
        CompletedQuotePDFOutput _nonGGPdf = new CompletedQuotePDFOutput();
        _nonGGPdf.Title = Resources.CarInsurance.PDF_Title;

        _nonGGPdf.QuoteRefTitle = QuoteTitle;
        _nonGGPdf.CustomerName = Name;
        _nonGGPdf.CarPlateNumber = VehicleNo;
        _nonGGPdf.Email = txtEmail.Text;
        _nonGGPdf.DOB = "";
        _nonGGPdf.Mobile = ContactNo;
        _nonGGPdf.Gender = "";
        _nonGGPdf.Occupation = "";

        _nonGGPdf.CurrentInsurer = Insurer;
        _nonGGPdf.CarMakeModel = CarMakeModel;
        _nonGGPdf.CarModelYear = CarModelYear;
        _nonGGPdf.AnyClaim = AnyClaim;
        _nonGGPdf.CommnecementDate = PolicyStartDate;
        _nonGGPdf.CommnecementEndDate = PolicyEndDate;
        _nonGGPdf.ModifyDate = ModifyDate;

        _nonGGPdf.SumInsuredAmount = SumInsured;
        _nonGGPdf.InsuranceType = InsuranceType;
        _nonGGPdf.GarageType = GarageType;

        _nonGGPdf.VehicleTypeUsage = VehicleType;
        _nonGGPdf.IncludeCTPL = CTPL;
        _nonGGPdf.NCB = "";
        _nonGGPdf.NamedDriver = AddDriver;
        _nonGGPdf.Driver1 = DriverFirst;
        _nonGGPdf.Driver2 = DriverTwo;
        _nonGGPdf.PolicyNumber = PolicyNumber;
        _nonGGPdf.Package = Package;
        _nonGGPdf.Premium = AnnualPremium;
        _nonGGPdf.CTPLAmount = CTPLAmount;
        _nonGGPdf.IdentificationNo = IdentificationNo;

        return _nonGGPdf;
    }

    #region Private Value

    private string AnnualPremium = "";
    private string InsuranceType = "";
    private string GarageType = "";
    private string CarModelYear = "";
    private string CarMakeModel = "";
    private string VehicleType = "";
    private string CTPL = "";
    private string SumInsured = "";
    private string ProvinceofRegistration = "";
    private string AnyClaim = "";
    private string AddDriver = "";
    private string CoverCommenceDate = "";
    private string Insurer = "";
    private string CTPLAmount = "";
    private string Package = "";
    private string Name = "";
    private string IdentificationNo = "";
    private string ContactNo = "";
    private string VehicleNo = "";
    private string PolicyStartDate = "";
    private string PolicyEndDate = "";
    private string ModifyDate = "";
    private string PaymentType = "";
    private string PaymentStatus = "";
    private string QuoteTitle = "";
    private string PolicyNumber = "";
    private string DriverFirst = "";
    private string DriverTwo = "";
    private string QRefNo = "";

    #endregion

    private void ShowDetails(Int64 _carId)
    {
        Int64 CustomerId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
        hidGetStep.Value = "1";
        DataSet _dsOffline = new DataSet();
        _dsOffline = GetCarDetails(_carId, CustomerId);
        if (_dsOffline != null && _dsOffline.Tables.Count > 2 && _dsOffline.Tables[2].Rows.Count > 0)
        {
            hidGetStep.Value = "2";
            ShowNonGGDetails(_dsOffline);
            return;
        }
        Int64 _refCEUserId = Convert.ToInt64(Convert.ToInt64(Session[General.UserId].ToString()));
        string _refNo = "";
        CarDetailTH _cardetailth = new CarDetailTH();
        CarDetailTHDB _cardetailthdb = new CarDetailTHDB();
        CE.Database.MyDatabase _db = new CE.Database.MyDatabase();
        DataSet _dsInsurance = new DataSet();

        Int64 _companyId = 0;

        System.Data.DataTable _dtSeletectItem = new System.Data.DataTable("SelectedResultItem");
        _dtSeletectItem = _db.GetDataTable("Select * from tblCarInsuranceSelectItemTH WHERE CarId = " + _carId.ToString());
        if (_dtSeletectItem != null && _dtSeletectItem.Rows.Count > 0)
        {
            _companyId = Convert.ToInt64(_dtSeletectItem.Rows[0]["ProviderCompanyId"].ToString());
            AnnualPremium = "$ " + Convert.ToSingle(_dtSeletectItem.Rows[0]["AnnualPremium"].ToString()).ToString("#.00");
        }
        CarInsuranceCompletedItemDB _carinsurancecompleteditemdb = new CarInsuranceCompletedItemDB();
        _dsInsurance = _carinsurancecompleteditemdb.GetCarInsuranceCompletedItem(_carId, _companyId);
        if (_dsInsurance != null && _dsInsurance.Tables[0].Rows.Count > 0)
        {
            switch (_dsInsurance.Tables[0].Rows[0]["InsuranceType"].ToString())
            {
                case "Class 1":
                    InsuranceType = Resources.CarInsurance.Insurance_ClassOne;
                    break;
                case "Class 1 (Low Cost)":
                    InsuranceType = Resources.CarInsurance.Insurance_ClassOneLowCost;
                    break;
                case "Class 2+":
                    InsuranceType = Resources.CarInsurance.Insurance_ClassTwoPlus;
                    break;
                case "Class 3+":
                    InsuranceType = Resources.CarInsurance.Insurance_ClassThreePlus;
                    break;
                case "Class 3":
                    InsuranceType = Resources.CarInsurance.Insurance_ClassThree;
                    break;
            }

            GarageType = _dsInsurance.Tables[0].Rows[0]["GarageType"].ToString() == "Authorized" ? Resources.CarInsurance.Garage_Authorized : Resources.CarInsurance.Garage_Dealer;
            CarModelYear = _dsInsurance.Tables[0].Rows[0]["CarModelYear"].ToString();
            CarMakeModel = _dsInsurance.Tables[0].Rows[0]["MakeModelDesc"].ToString();
            VehicleType = _dsInsurance.Tables[0].Rows[0]["VehicleType"].ToString() == "Private Use" ? Resources.CarInsurance.VehicleType_Private : Resources.CarInsurance.VehicleType_Commercial;
            SumInsured = Convert.ToSingle(_dsInsurance.Tables[0].Rows[0]["SumInsured"]).ToString("#,###") + " " + Resources.CarInsurance.Baht;
            ProvinceofRegistration = _dsInsurance.Tables[0].Rows[0]["ProvinceRegistration"].ToString() == "Bangkok Metro Area" ? Resources.CarInsurance.Province_BKK : Resources.CarInsurance.Province_UPC;
            AnyClaim = _dsInsurance.Tables[0].Rows[0]["AnyClaim"].ToString() == "Yes" ? Resources.CarInsurance.Option_YesHave : Resources.CarInsurance.Option_NoHave;
            AddDriver = _dsInsurance.Tables[0].Rows[0]["Driver"].ToString() == "Yes" ? Resources.CarInsurance.Option_YesSpecify : Resources.CarInsurance.Option_NoSpecify;
            if (_dsInsurance.Tables[0].Rows[0]["CommenceDate"] != DBNull.Value)
            {
                CoverCommenceDate = Convert.ToDateTime(_dsInsurance.Tables[0].Rows[0]["CommenceDate"]).ToString("dd-MMM-yyyy");
            }

            if (_dsInsurance.Tables.Count > 1)
            {
                if (_dsInsurance.Tables[1].Rows.Count > 0)
                {
                    object moddate = _db.ExecuteScalarSqlStatement("SELECT Modifieddate FROM tblCarPolicy WHERE CECarId = " + _carId.ToString());
                    if (moddate != null)
                        ModifyDate = Convert.ToDateTime(moddate).ToString("dd-MMM-yyyy", new System.Globalization.CultureInfo("en-US"));

                    if (_dsInsurance.Tables[1].Rows[0]["CoverTypeDesc"].ToString() != "GluayGluay.com Special")
                    {
                        Insurer = GetInsurer(_companyId);
                    }
                    else
                    {
                        Insurer = "";
                    }
                    AnnualPremium = Convert.ToSingle(_dsInsurance.Tables[1].Rows[0]["AnnualPremium"]).ToString("#,###.00") + " " + Resources.CarInsurance.Baht;
                    CTPL = _dsInsurance.Tables[1].Rows[0]["CTPL"].ToString() == "Yes" ? Resources.CarInsurance.Option_Yes : Resources.CarInsurance.Option_No;
                    CTPLAmount = Convert.ToSingle(_dsInsurance.Tables[1].Rows[0]["CTPLAmount"]).ToString("#.00") + " " + Resources.CarInsurance.Baht;
                    Package = _dsInsurance.Tables[1].Rows[0]["CoverTypeDesc"].ToString();

                    Name = _dsInsurance.Tables[1].Rows[0]["Name"].ToString();
                    IdentificationNo = "";
                    if (_dsInsurance.Tables[1].Rows[0]["IdentificationNo"] != null)
                    {
                        IdentificationNo = _dsInsurance.Tables[1].Rows[0]["IdentificationNo"].ToString();
                    }
                    ContactNo = String.Format("{0:0##-###-####}", Convert.ToInt32(_dsInsurance.Tables[1].Rows[0]["ContactNo"]));
                    VehicleNo = _dsInsurance.Tables[1].Rows[0]["VehicleNo"].ToString();
                    if (_dsInsurance.Tables[1].Rows[0]["PolicyStartDate"] != DBNull.Value)
                    {
                        PolicyStartDate = Convert.ToDateTime(_dsInsurance.Tables[1].Rows[0]["PolicyStartDate"]).ToString("dd-MMM-yyyy");
                        PolicyEndDate = Convert.ToDateTime(_dsInsurance.Tables[1].Rows[0]["PolicyStartDate"]).AddYears(1).ToString("dd-MMM-yyyy");
                    }
                    _refNo = _dsInsurance.Tables[1].Rows[0]["QuoteRefNo"].ToString();

                    switch (_dsInsurance.Tables[1].Rows[0]["PaymentType"].ToString())
                    {
                        case "CreditCard":
                            PaymentType = Resources.ProposalForm.CreditCard;
                            break;
                        case "Bank In":
                            PaymentType = Resources.ProposalForm.BankIn;
                            break;
                    }
                    switch (_dsInsurance.Tables[1].Rows[0]["PaymentStatus"].ToString())
                    {
                        case "True":
                            PaymentStatus = Resources.ProposalForm.Yes;
                            break;
                        case "False":
                            PaymentStatus = Resources.ProposalForm.No;
                            break;
                    }
                    if (_dsInsurance.Tables[1].Rows[0]["PaymentType"].ToString() == "CreditCard")
                    {
                        switch (_dsInsurance.Tables[1].Rows[0]["PaymentCode"].ToString())
                        {
                            case "0":
                                PaymentStatus += " - " + Resources.ProposalForm.Fail;
                                break;
                            case "1":
                                PaymentStatus += " - " + Resources.ProposalForm.Success;
                                break;
                            case "2":
                                PaymentStatus += " - " + Resources.ProposalForm.Cancel;
                                break;
                        }
                    }
                }
            }
            QuoteTitle = Resources.CarInsurance.QuoteRefNo + " : " + _refNo + " - " + Resources.CarInsurance.CarInsuranceList + " " + _dsInsurance.Tables[0].Rows[0]["MakeModelDesc"].ToString(); ;
            QRefNo = _refNo;
        }
    }

    private void ShowNonGGDetails(DataSet DsNonGG)
    {
        if (DsNonGG != null && DsNonGG.Tables.Count > 2 && DsNonGG.Tables[2].Rows.Count > 0)
        {
            Int64 _refCEUserId = Convert.ToInt64(Convert.ToInt64(Session[General.UserId].ToString()));
            string _refNo = "";
            Int64 _companyId = Convert.ToInt64(DsNonGG.Tables[2].Rows[0]["ProviderCompanyId"].ToString());
            AnnualPremium = "$ " + Convert.ToSingle(DsNonGG.Tables[2].Rows[0]["TotalCollectedPremium"].ToString()).ToString("#.00");

            switch (DsNonGG.Tables[2].Rows[0]["InsuranceType"].ToString())
            {
                case "10":
                    InsuranceType = Resources.CarInsurance.Insurance_ClassOne;
                    break;
                case "11":
                    InsuranceType = Resources.CarInsurance.Insurance_ClassOneLowCost;
                    break;
                case "21":
                    InsuranceType = Resources.CarInsurance.Insurance_ClassTwoPlus;
                    break;
                case "31":
                    InsuranceType = Resources.CarInsurance.Insurance_ClassThreePlus;
                    break;
                case "30":
                    InsuranceType = Resources.CarInsurance.Insurance_ClassThree;
                    break;
            }

            GarageType = DsNonGG.Tables[2].Rows[0]["GarageType"].ToString() == "1" ? Resources.CarInsurance.Garage_Authorized : Resources.CarInsurance.Garage_Dealer;
            CarModelYear = DsNonGG.Tables[2].Rows[0]["CarYear"].ToString();
            CarMakeModel = DsNonGG.Tables[2].Rows[0]["CarModel"].ToString();
            VehicleType = DsNonGG.Tables[2].Rows[0]["VehicleType"].ToString() == "110" ? Resources.CarInsurance.VehicleType_Private : Resources.CarInsurance.VehicleType_Commercial;
            SumInsured = Convert.ToSingle(DsNonGG.Tables[0].Rows[0]["SumInsured"]).ToString("#,###") + " " + Resources.CarInsurance.Baht;
            ProvinceofRegistration = DsNonGG.Tables[2].Rows[0]["CarPlateProvince"].ToString();
            AnyClaim = Resources.CarInsurance.Option_NoHave;
            if (DsNonGG.Tables[2].Rows[0]["IsClaim"] != DBNull.Value && DsNonGG.Tables[2].Rows[0]["IsClaim"].ToString() != string.Empty)
            {
                AnyClaim = DsNonGG.Tables[2].Rows[0]["IsClaim"].ToString() == "1" ? Resources.CarInsurance.Option_YesHave : Resources.CarInsurance.Option_NoHave;
            }
            AddDriver = DsNonGG.Tables[2].Rows[0]["NoOfDriver"].ToString() != "0" ? Resources.CarInsurance.Option_YesSpecify : Resources.CarInsurance.Option_NoSpecify;
            CoverCommenceDate = Convert.ToDateTime(DsNonGG.Tables[2].Rows[0]["CoverageStart"]).ToString("dd-MMM-yyyy");

            if (DsNonGG.Tables[2].Rows[0]["Package"].ToString() != "GluayGluay.com Special")
            {
                Insurer = GetInsurer(Int32.Parse(DsNonGG.Tables[2].Rows[0]["ProviderCompanyId"].ToString())); //DsNonGG.Tables[2].Rows[0]["CurrentInsurer"].ToString();
                Package = DsNonGG.Tables[2].Rows[0]["Package"].ToString();
            }
            else
            {
                Insurer = "";
                Package = DsNonGG.Tables[2].Rows[0]["Package"].ToString() + " (" + Resources.CarInsurance.SpecialNCB + ")";
            }

            AnnualPremium = Convert.ToSingle(DsNonGG.Tables[2].Rows[0]["TotalCollectedPremium"]).ToString("#,###.00") + " " + Resources.CarInsurance.Baht;
            CTPL = DsNonGG.Tables[2].Rows[0]["IsCTPL"].ToString() == "True" ? Resources.CarInsurance.Option_Yes : Resources.CarInsurance.Option_No;
            CTPLAmount = Convert.ToSingle(DsNonGG.Tables[2].Rows[0]["CTPL"]).ToString("#.00") + " " + Resources.CarInsurance.Baht;
            

            Name = DsNonGG.Tables[2].Rows[0]["Name"].ToString();
            IdentificationNo = "N/A";

            ContactNo = String.Format("{0:0##-###-####}", Convert.ToInt32(DsNonGG.Tables[2].Rows[0]["ContactNo"]));
            VehicleNo = DsNonGG.Tables[2].Rows[0]["CarPlate"].ToString();
            PolicyStartDate = Convert.ToDateTime(DsNonGG.Tables[2].Rows[0]["CoverageStart"]).ToString("dd-MMM-yyyy"); //CTPLStart
            PolicyEndDate = Convert.ToDateTime(DsNonGG.Tables[2].Rows[0]["CoverageStart"]).AddYears(1).ToString("dd-MMM-yyyy"); //CTPLStart
            ModifyDate = Convert.ToDateTime(DsNonGG.Tables[2].Rows[0]["Modifieddate"]).ToString("dd-MMM-yyyy", new System.Globalization.CultureInfo("en-US"));
            _refNo = DsNonGG.Tables[2].Rows[0]["QuoteRefNo"].ToString();

            PolicyNumber = GetPolicyNumber(Int32.Parse(DsNonGG.Tables[2].Rows[0]["RefCEUserId"].ToString()),
                    Int32.Parse(DsNonGG.Tables[2].Rows[0]["CECarId"].ToString()),
                    DsNonGG.Tables[2].Rows[0]["QuoteRefNo"].ToString());

            if (DsNonGG.Tables[3].Rows.Count > 0)
            {
                DriverFirst = DsNonGG.Tables[3].Rows[0]["FullName"].ToString();
            }
            if (DsNonGG.Tables[3].Rows.Count > 1)
            {
                DriverTwo = DsNonGG.Tables[3].Rows[1]["FullName"].ToString();
            }

            PaymentType = "N/A";
            switch (DsNonGG.Tables[2].Rows[0]["PaymentStatus"].ToString())
            {
                case "1":
                    PaymentStatus = Resources.ProposalForm.Yes;
                    break;
                case "0":
                    PaymentStatus = Resources.ProposalForm.No;
                    break;
            }
            QuoteTitle = Resources.CarInsurance.QuoteRefNo + " : " + _refNo + " - " + Resources.CarInsurance.CarInsuranceList + " " + DsNonGG.Tables[2].Rows[0]["CarModel"].ToString();
            QRefNo = _refNo;
        }
    }

    public string GetInsurer(Int64 CompanyId)
    {
        MyDatabase _db = new MyDatabase();
        object CompanyName;
        if (CE.Global.General.SetCulture() == "th-TH")
        {
            CompanyName = _db.ExecuteScalarSqlStatement("SELECT THCompanyNameDescription FROM tblProviderCompany WHERE ProviderCompanyId = " + CompanyId.ToString());
        }
        else
        {
            CompanyName = _db.ExecuteScalarSqlStatement("SELECT CompanyNameDescription FROM tblProviderCompany WHERE ProviderCompanyId = " + CompanyId.ToString());
        }

        string _Insurer = "";
        if (CompanyName != null)
        {
            _Insurer = CompanyName.ToString();
        }

        return _Insurer;
    }

    public string GetPolicyNumber(Int32 RefCEUserId, Int32 CECarId, string QuoteRefNo)
    {
        MyDatabase _db = new MyDatabase();
        object PolicyNumber;
        PolicyNumber = _db.ExecuteScalarSqlStatement("SELECT Remarks FROM tblNonGGTransaction WHERE TransactionTypeID = 400 AND RefCEUserId = " + RefCEUserId.ToString() + " AND CECarId = " + CECarId.ToString() + " AND QuoteRefNo = '" + QuoteRefNo + "'");

        string _PolicyNumber = "";
        if (PolicyNumber != null)
        {
            _PolicyNumber = PolicyNumber.ToString();
        }

        return _PolicyNumber;
    }

    private Int64 CheckCompleteStatus(Int64 _resultId)
    {
        UserResultHistory _result = new UserResultHistory();
        UserResultHistoryDB _resultdb = new UserResultHistoryDB();
        Int64 _carId = 0;
        Int64 CustomerId = 0;
        if (Session[CE.Global.General.UserId] != null)
        {
            CustomerId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
        }
        _result = _resultdb.GetResultHistory(Convert.ToInt64(Session[General.UserId].ToString()), _resultId);
        if (_result != null && _result.IsComplete)
        {
            _carId = _result.ID1;
        }
        else
        {
            DataSet _ds = GetCarDetails(_result.ID1, CustomerId);
            if (_ds != null && _ds.Tables.Count > 2 && _ds.Tables[2].Rows.Count > 0)
            {
                _carId = _result.ID1;
            }
        }

        return _carId;
    }

    public DataSet GetCarDetails(Int64 CarId, Int64 CustomerId)
    {
        MyDatabase _db = new MyDatabase();
        string _spName = "[sprocCXCRMNONGGPackageCarDetailsSelectedItemByCarId]";

        SortedList _spara = new SortedList();
        _spara.Add("@CarId", CarId);
        _spara.Add("@CustomerId", CustomerId);

        DataSet _ds = _db.GetDataSet(_spName, _spara);

        return _ds;
    }

}
